package com.hyts.classcode.enhance.bytebuddy.factory;

import com.hyts.classcode.enhance.bytebuddy.agent.ClassAgentTransformer;
import com.hyts.classcode.enhance.bytebuddy.listener.ClassAgentListener;
import net.bytebuddy.agent.builder.AgentBuilder;
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.matcher.ElementMatcher;
import net.bytebuddy.matcher.ElementMatchers;
import java.lang.instrument.Instrumentation;

/**
 * project-name:coffice-toolbox
 * package-name:com.hyts.enhance.bytebuddy.factory
 * author:Libo/Alex
 * create-date:2019-06-30 01:20
 * copyright:libo-hyts-github
 * email:libo2dev@aliyun.com
 * description:此类主要用于: byte
 */
public class ByteBuddyAgentFactory {



    /**
     * 构建bytebuddy对象返回的class
     * @return
     */
    public static void create(Instrumentation instrumentation, ClassAgentTransformer classAgentTransformer,
                              ClassAgentListener classAgentListener){
        ElementMatcher<? super TypeDescription> type = classAgentTransformer.classNameMatcher() == null ?
                ElementMatchers.any():classAgentTransformer.classNameMatcher();
        new AgentBuilder.Default().
                 type(type).
                 transform(classAgentTransformer).
                 with(classAgentListener).
                 installOn(instrumentation);
    }

}
